import { IFeidaoAiWeappPage } from '@dfeidao/atom-wxapp/interfaces';
import get_local from '@dfeidao/atom-wxapp/local/get';
import set_local from '@dfeidao/atom-wxapp/local/set';
import get_global from '@dfeidao/atom-wxapp/global/get';
import show_modal from '@dfeidao/atom-wxapp/api/interaction/show-modal';
import nodejs from '@dfeidao/atom-wxapp/msg/nodejs';
import show_toast from '@dfeidao/atom-wxapp/api/interaction/show-toast';
import show_nav_bar_loading from '@dfeidao/atom-wxapp/api/interaction/show-nav-bar-loading';
import showLoadling from '@dfeidao/atom-wxapp/api/interaction/show-loading';
import hideLoadling from '@dfeidao/atom-wxapp/api/interaction/hide-loading';
// import { send_msg as url } from '../atom/config';
// import a007 from './a007';

export default async function a006(fd: IFeidaoAiWeappPage, e: {
	detail: {
		value: string;
	}
}) {
	// todo
	const userInfo = get_global('userinfo') as any;
	const doccode = get_local(fd, 'doccode') as any;
	const dataArray = get_local(fd, 'dataArray') as any[];
	const save_data = dataArray.filter((i) => {
		return i.out_inv_qty !== 0;
	});
	console.log(save_data);
	if (save_data.length === 0) {
		show_toast('没有可以保存的数据', 'none', 2000);
		return;
	}
	console.log({
		dataArray: save_data,
		userInfo: {
			user_code: userInfo.usercode
		},
		doccode: doccode || ''
	}, '----------参数-----------')
	showLoadling('保存中');
	show_nav_bar_loading();
	try {
		const data = await nodejs<any>('app-output-det-save', {
			dataArray: save_data,
			userInfo: {
				user_code: userInfo.usercode
			},
			doccode: doccode || ''
		});
		console.log(data, '--------data---------')
		if (data.msg === 'success') {
			set_local(fd, 'doccode', data.doccode);
			// 过账
			// a007(fd);
			return;
		}
		throw data.msg;
	} catch (error) {
		console.log(error, '---------err---------')
		hideLoadling();
		const succ = await show_modal('保存失败', error.toString());
		if (succ.msg.confirm) {
			console.log('用户点击确定')
		} else if (succ.msg.cancel) {
			console.log('用户点击取消')
		}
	}
}
